/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Helper.PatientHelper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author hxbien
 */
public class PatientDAO {

    public static ArrayList<PatientHelper> FillAll(Connection cnn) throws Exception {
        ArrayList<PatientHelper> arrPatientList = new ArrayList<PatientHelper>();
        PatientHelper PatientData = null;
        String strSQL = "Select * from tblPatient";
        PreparedStatement stmt = cnn.prepareStatement(strSQL);
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            PatientData = new PatientHelper();
            PatientData.setPatientId(rs.getString("PatientId"));
            PatientData.setPatientName(rs.getString("PatientName"));
            PatientData.setPatientAddress(rs.getString("PatientAddress"));
            int gen = Integer.parseInt(rs.getString("PatientGender"));
            if (gen == 1) {
                PatientData.setPatientGender("Male");
            } else {
                PatientData.setPatientGender("FeMale");
            }

            PatientData.setPatientAge(rs.getString("PatientAge"));
            PatientData.setPatientPhone(rs.getString("PatientPhone"));
            PatientData.setPatientDescription(rs.getString("PatientDescription"));
            arrPatientList.add(PatientData);
        }
        return arrPatientList;
    }

    public static ResultSet FillAllPatient(Connection cnn) throws Exception {
        String strSQL = "Select * from tblPatient";
        PreparedStatement stmt = cnn.prepareStatement(strSQL);
        return stmt.executeQuery();
    }

    public static ResultSet FillOne(Connection cnn, PatientHelper obj) throws SQLException {
        String strSQL = "Select * from tblPatient where PatientId=?";
        PreparedStatement stmt = cnn.prepareStatement(strSQL);
        stmt.setString(1, obj.getPatientId());
        return stmt.executeQuery();
    }

    public static int Insert(Connection cnn, PatientHelper obj) throws Exception {
        String strSQL = "Insert into tblPatient(PatientName, PatientAddress, PatientGender, PatientAge, PatientPhone, PatientDescription) values(?,?,?,?,?,?)";
        PreparedStatement stmt = cnn.prepareStatement(strSQL);
        stmt.setString(1, obj.getPatientName());
        stmt.setString(2, obj.getPatientAddress());
        stmt.setString(3, obj.getPatientGender());
        stmt.setString(4, obj.getPatientAge());
        stmt.setString(5, obj.getPatientPhone());
        stmt.setString(6, obj.getPatientDescription());
        return stmt.executeUpdate();
    }

    public static boolean checkPatient(Connection cnn, PatientHelper obj) throws Exception {
        boolean check = false;
        String strSQL = "Select * from tblPatient where PatientName=?";
        PreparedStatement stmt = cnn.prepareStatement(strSQL);
        stmt.setString(1, obj.getPatientName());
        ResultSet rs = stmt.executeQuery();
        if (rs.next()) {
            check = true;
        }
        return check;
    }

    public static int Update(Connection cnn, PatientHelper obj) throws Exception {
        String strSQL = "update tblPatient set PatientName=?,PatientAddress=?,PatientGender=?,PatientAge=?, PatientPhone=?, PatientDescription=? where PatientId=?";
        PreparedStatement stmt = cnn.prepareStatement(strSQL);
        stmt.setString(1, obj.getPatientName());
        stmt.setString(2, obj.getPatientAddress());
        stmt.setString(3, obj.getPatientGender());
        stmt.setString(4, obj.getPatientAge());
        stmt.setString(5, obj.getPatientPhone());
        stmt.setString(6, obj.getPatientDescription());
        stmt.setString(7, obj.getPatientId());
        return stmt.executeUpdate();
    }

    public static int Delete(Connection cnn, PatientHelper obj) throws Exception {
        String strSQL = "DELETE FROM tblPatient WHERE PatientId = ?";
        PreparedStatement stmt = cnn.prepareStatement(strSQL);
        stmt.setString(1, obj.getPatientId());
        return stmt.executeUpdate();
    }

    public static ArrayList<PatientHelper> SearchPa(Connection cnn, PatientHelper obj) throws SQLException {
        ArrayList<PatientHelper> arrPatientList = new ArrayList<PatientHelper>();
        PatientHelper PatientData = null;
        String strSQL = "Select * from tblPatient where PatientName like ?";
        PreparedStatement stmt = cnn.prepareStatement(strSQL);
        stmt.setString(1, "%"+obj.getPatientName()+"%");
        ResultSet rs = stmt.executeQuery();
        while (rs.next()) {
            PatientData = new PatientHelper();
            PatientData.setPatientId(rs.getString("PatientId"));
            PatientData.setPatientName(rs.getString("PatientName"));
            PatientData.setPatientAddress(rs.getString("PatientAddress"));
            int gen = Integer.parseInt(rs.getString("PatientGender"));
            if (gen == 1) {
                PatientData.setPatientGender("Male");
            } else {
                PatientData.setPatientGender("FeMale");
            }

            PatientData.setPatientAge(rs.getString("PatientAge"));
            PatientData.setPatientPhone(rs.getString("PatientPhone"));
            PatientData.setPatientDescription(rs.getString("PatientDescription"));
            arrPatientList.add(PatientData);
        }
        return arrPatientList;
    }
}
